﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data.Linq;

namespace LMS.SMSH.WEB
{
    public partial class AdmIsuueNotice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 根据参数InfoId，读取Info数据表信息显示到页面，以便于编辑信息
                //如果 页面参数info不为空,那就显示到文本框
                if (Request.QueryString["InfoId"] != null)
                {
                    var query = LMSDataContext.GetLMSDB.TB_Info.Where(info => info.infoId == Int32.Parse(Request.QueryString["InfoId"]));
                    TB_Info tb_info = query.ToArray().First();
                    txtCaption.Text = tb_info.infotitle;
                    txtContent.Text = tb_info.infoDetail;
                    ddlInfoType.SelectedItem.Text = (tb_info.infoType == true) ? "紧急通知" : "普通通知";
                }
                #endregion
            }
        }

        /// <summary>
        /// 将信息写入到Info数据表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            #region 根据页面参数InfoID，判断是发布消息还是更新消息
            bool infoType = (ddlInfoType.SelectedItem.Text == "紧急通知") ? true : false;
            if (Request.QueryString["InfoId"] == null)
            {
                //表示 管理员发布一条信息
                LMSDataContext.GetLMSDB.sp_addInfo(txtCaption.Text.Trim(), infoType, txtContent.Text.Trim());
                string strMsg = string.Format("alert('信息发布成功！');window.location.href='AdmNoticeManage.aspx'");
                this.ClientScript.RegisterStartupScript(this.GetType(), "", strMsg, true);
            }
            else
            {
                //表示管理员更新一条信息
                TB_Info tb_info = LMSDataContext.GetLMSDB.TB_Info.Single(info => info.infoId == Int32.Parse(Request.QueryString["InfoId"]));
                tb_info.infotitle = txtCaption.Text.Trim();
                tb_info.infoType = infoType;
                tb_info.infoDetail = txtContent.Text.Trim();
                LMSDataContext.GetLMSDB.SubmitChanges();
                string strMsg = string.Format("alert('信息更新成功！');window.location.href='AdmNoticeManage.aspx'");
            } 
            #endregion
        }


    }
}